package com.lottery.material.service.admin.circle.impl;

import com.lottery.material.dao.circle.CircleMemberInfoMapper;
import com.lottery.material.enumeration.circle.CircleInfoEnum;
import com.lottery.material.enumeration.circle.CircleMemberInfoEnum;
import com.lottery.material.pojo.circle.CircleMemberInfo;
import com.lottery.material.pojo.page.Page;
import com.lottery.material.pojocustom.circle.CircleMemberInfoCustom;
import com.lottery.material.service.admin.circle.CircleMemberInfoService;
import com.lottery.material.util.ArrayUtil;
import com.lottery.material.util.CalendarUtil;
import com.lottery.material.util.EnumUtil;
import com.lottery.material.util.ListUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description：圈子成员服务接口实现类
 * Created by 朱正磊 on 2019-04-13 15:36
 */
@Slf4j
@Service("adminCircleMemberInfoService")
public class CircleMemberInfoServiceImpl implements CircleMemberInfoService {

    private final CircleMemberInfoMapper circleMemberInfoMapper;

    @Autowired
    public CircleMemberInfoServiceImpl(CircleMemberInfoMapper circleMemberInfoMapper) {
        this.circleMemberInfoMapper = circleMemberInfoMapper;
    }

    /**
     * description：新增一条数据
     * user 朱正磊
     * time 2019-04-20 15:14
     * @param circleMemberInfo 对象
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer add(CircleMemberInfo circleMemberInfo) {
        return circleMemberInfoMapper.insertSelective(circleMemberInfo);
    }

    /**
     * description：根据主键物理删除
     * user 朱正磊
     * time 2019-04-20 15:13
     * @param id 主键
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer deleteById(Integer id) {
        return circleMemberInfoMapper.deleteByPrimaryKey(id);
    }

    /**
     * description：根据主键查询唯一数据
     * user 朱正磊
     * time 2019-04-20 15:15
     * @param id 主键
     * @return 返回唯一数据
     */
    @Override
    public CircleMemberInfo findById(Integer id) {
        return circleMemberInfoMapper.selectByPrimaryKey(id);
    }

    /**
     * description：根据主键修改一条数据
     * user 朱正磊
     * time 2019-04-20 15:16
     * @param circleMemberInfo 对象
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer updateById(CircleMemberInfo circleMemberInfo) {
        return circleMemberInfoMapper.updateByPrimaryKeySelective(circleMemberInfo);
    }

    /**
     * description：根据用户id查询圈子成员列表
     * user 朱正磊
     * time 2019-04-22 15:11
     * @param userId 用户ID
     * @return 返回用户id查询圈子成员列表
     */
    @Override
    public List<CircleMemberInfo> findByUserId(String userId) {
        return circleMemberInfoMapper.findByUserId(userId);
    }

    /**
     * description：根据圈子ID和圈内等级查询圈子成员列表
     * user 朱正磊
     * time 2019-04-22 15:57
     * @param circleId 圈子ID
     * @param rank 圈内等级
     * @return 返回圈子成员列表
     */
    @Override
    public List<CircleMemberInfo> findByCircleIdAndRank(Integer circleId, Integer rank) {
        return circleMemberInfoMapper.findByCircleIdAndRank(circleId, rank);
    }

    /**
     * description：查询扩展类分页列表
     * user 朱正磊
     * time 2019-04-17 17:01
     * @param page 分页对象
     * @return 返回扩展类分页列表数据
     */
    @Override
    public List<CircleMemberInfoCustom> findCustomListPage(Page page) {
        List<CircleMemberInfoCustom> list = circleMemberInfoMapper.findCustomListPage(page);
        list.forEach(this::setCustom);
        return list;
    }

    /**
     * description：根据主键查询唯一扩展类数据
     * user 朱正磊
     * time 2019-04-17 17:03
     * @param id 主键
     * @return 返回唯一扩展类数据
     */
    @Override
    public CircleMemberInfoCustom findCustomById(Integer id) {
        CircleMemberInfoCustom custom = circleMemberInfoMapper.findCustomById(id);
        this.setCustom(custom);
        return custom;
    }

    /**
     * description：设置扩展类的扩展属性
     * user 朱正磊
     * time 2019-04-17 17:48
     * @param custom 扩展类对象
     */
    private void setCustom(CircleMemberInfoCustom custom) {
        //创建时间
        custom.setCreateTimeFormat(CalendarUtil.dateToString(custom.getCreateTime()));
        //置顶时间
        custom.setTopTimeFormat(CalendarUtil.dateToString(custom.getTopTime()));
        //最后一次发帖时间
        custom.setLastPostTimeFormat(CalendarUtil.dateToString(custom.getLastPostTime()));
        //是否禁言（1：否；2：是）
        custom.setIsBanDesc(EnumUtil.getDescriptionByCodeValue(CircleMemberInfoEnum.IS_BAN,
                custom.getIsBan(), CircleMemberInfoEnum.class));
        //是否置顶（1：否；2：是）
        custom.setIsTopDesc(EnumUtil.getDescriptionByCodeValue(CircleMemberInfoEnum.IS_TOP,
                custom.getIsTop(), CircleMemberInfoEnum.class));
        //圈内等级（1：普通圈员/预备圈员；2：正式圈员/认证圈员；3：副圈主；4：圈主）
        custom.setRankDesc(EnumUtil.getDescriptionByCodeValue(CircleMemberInfoEnum.RANK,
                custom.getRank(), CircleMemberInfoEnum.class));
    }

    /**
     * description：批量禁言
     * user 朱正磊
     * time 2019-04-24 16:37
     * @param ids 主键集合(多个id以,进行拼接)
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer updateBatchBan(String ids) {
        Map<String, Object> data = new HashMap<>();
        data.put("idList", ListUtil.getIdList(ids));
        data.put("isBan", CircleMemberInfoEnum.IS_BAN_TRUE.getValue());
        return circleMemberInfoMapper.updateBatchBanOrNot(data);
    }

    /**
     * description：批量取消禁言
     * user 朱正磊
     * time 2019-04-24 16:37
     * @param ids 主键集合(多个id以,进行拼接)
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer updateBatchNotBan(String ids) {
        Map<String, Object> data = new HashMap<>();
        data.put("idList", ListUtil.getIdList(ids));
        data.put("isBan", CircleMemberInfoEnum.IS_BAN_FALSE.getValue());
        return circleMemberInfoMapper.updateBatchBanOrNot(data);
    }

}
